package com.lzk.jetpacktest.event.scroll

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import android.view.VelocityTracker
import androidx.databinding.DataBindingUtil
import com.lzk.jetpacktest.R
import com.lzk.jetpacktest.databinding.ActivityEventBinding

class EventActivity : AppCompatActivity() {

    private lateinit var mBinding: ActivityEventBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_event)

        initEvent()
    }

    private fun initEvent(){
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        val velocityTracker = VelocityTracker.obtain().apply {
            addMovement(event)
            computeCurrentVelocity(1000)
        }
        if (event != null && event.action == MotionEvent.ACTION_UP){
            val x = velocityTracker.xVelocity
            val y = velocityTracker.yVelocity
            val xStr = "水平速度:${x}"
            val yStr = "垂直速度:${y}"
            mBinding.tvTouchEvent.text = "$xStr \n $yStr"
        }
        velocityTracker.clear()
        velocityTracker.recycle()
        return super.onTouchEvent(event)
    }
}